package com.dinghui.salary.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dinghui.salary.mapper.SalaryCalculateEquationMapper;
import com.dinghui.salary.pojo.Page;
import com.dinghui.salary.pojo.SalaryCalculateEquation;
import com.dinghui.salary.pojo.SalaryType;
import com.dinghui.salary.service.SalaryCalculateEquationService;
import com.dinghui.salary.utils.PagedGridResult;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc
 */
@Slf4j
@Service(version = "2.0.0")
public class SalaryCalculateEquationServiceImpl extends BaseService implements SalaryCalculateEquationService {

    @Autowired
    private SalaryCalculateEquationMapper Mapper;

    @Override
    public int add(SalaryCalculateEquation mouthType) {
        Example example = new Example(SalaryCalculateEquation.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(mouthType.getName())) {
            criteria.andEqualTo("name", mouthType.getName());
        }
        criteria.andEqualTo("isDelete",  0);
        PageHelper.startPage(1, 100);
        List<SalaryCalculateEquation> list = Mapper.selectByExample(example);

        if(list.size() > 0){
            return -5;
        }else{
            int i = Mapper.insertSelective(mouthType);
            return i;
        }
    }
    public int modify(SalaryCalculateEquation mouthType) {
        Example example = new Example(SalaryType.class);
        example.createCriteria().andEqualTo("id",mouthType.getId());

        Example example1 = new Example(SalaryCalculateEquation.class);
        Example.Criteria criteria1 = example1.createCriteria();
        if (StringUtils.isNotBlank(mouthType.getName())) {
            criteria1.andEqualTo("name", mouthType.getName());
        }
        criteria1.andEqualTo("isDelete",  0);
        PageHelper.startPage(1, 10);
        List<SalaryCalculateEquation> list = Mapper.selectByExample(example1);

        if(list.size() > 0){
            for(SalaryCalculateEquation item : list){
                if(!item.getId().equals(mouthType.getId())){
                    return -5;
                }else{
                    int i = Mapper.updateByExampleSelective(mouthType,example);
                    return i;
                }
            }
        }else{
            int i = Mapper.updateByExampleSelective(mouthType,example);
            return i;
        }
        return 0;
    }

    @Override
    public List<SalaryCalculateEquation> getAllList() {
        return Mapper.selectAll();
    }


    @Override
    public Object Delete(SalaryCalculateEquation dto) {
        Example example = new Example(SalaryType.class);
        example.createCriteria().andEqualTo("id",dto.getId());
        int i = Mapper.updateByExampleSelective(dto,example);
        return i;
    }

    @Override
    public PagedGridResult FindAll(Page dto) {

        Example example = new Example(SalaryCalculateEquation.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(dto.getName())) {
            criteria.andLike("name", "%" + dto.getName() + "%");
        }

        /*if (UserStatus.isUserStatusValid(status)) {
            criteria.andEqualTo("activeStatus", status);
        }*/

        if (dto.getStartDate() != null) {
            criteria.andGreaterThanOrEqualTo("createTime", dto.getStartDate());
        }
        if (dto.getEndDate() != null) {
            criteria.andLessThanOrEqualTo("createTime", dto.getEndDate());
        }
        criteria.andEqualTo("isDelete",  0);
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<SalaryCalculateEquation> list = Mapper.selectByExample(example);

        return setterPagedGrid(list, dto.getPage());
    }

    @Override
    public Object FindById(SalaryCalculateEquation dto) {

        return Mapper.selectOne(dto);
    }
}
